package com.papers.juan.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.papers.common.annotation.Log;
import com.papers.common.annotation.RepeatSubmit;
import com.papers.common.core.controller.BaseController;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.domain.R;
import com.papers.common.core.page.TableDataInfo;
import com.papers.common.core.validate.AddGroup;
import com.papers.common.core.validate.EditGroup;
import com.papers.common.enums.BusinessType;
import com.papers.common.utils.poi.ExcelUtil;
import com.papers.juan.domain.bo.TJuanPaperQuestionBo;
import com.papers.juan.domain.vo.TJuanPaperQuestionVo;
import com.papers.juan.service.ITJuanPaperQuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 试卷题目
 *
 * @author papers
 * @date 2022-10-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/juan/juanPaperQuestion")
public class TJuanPaperQuestionController extends BaseController {

    private final ITJuanPaperQuestionService iTJuanPaperQuestionService;

    /**
     * 查询试卷题目列表
     */
    @SaCheckPermission("juan:juanPaperQuestion:list")
    @GetMapping("/list")
    public TableDataInfo<TJuanPaperQuestionVo> list(TJuanPaperQuestionBo bo, PageQuery pageQuery) {
        return iTJuanPaperQuestionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出试卷题目列表
     */
    @SaCheckPermission("juan:juanPaperQuestion:export")
    @Log(title = "试卷题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TJuanPaperQuestionBo bo, HttpServletResponse response) {
        List<TJuanPaperQuestionVo> list = iTJuanPaperQuestionService.queryList(bo);
        ExcelUtil.exportExcel(list, "试卷题目", TJuanPaperQuestionVo.class, response);
    }

    /**
     * 获取试卷题目详细信息
     *
     * @param paperId 主键
     */
    @SaCheckPermission("juan:juanPaperQuestion:query")
    @GetMapping("/{paperId}")
    public R<TJuanPaperQuestionVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable String paperId) {
        return R.ok(iTJuanPaperQuestionService.queryById(paperId));
    }

    /**
     * 新增试卷题目
     */
    @SaCheckPermission("juan:juanPaperQuestion:add")
    @Log(title = "试卷题目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TJuanPaperQuestionBo bo) {
        return toAjax(iTJuanPaperQuestionService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改试卷题目
     */
    @SaCheckPermission("juan:juanPaperQuestion:edit")
    @Log(title = "试卷题目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TJuanPaperQuestionBo bo) {
        return toAjax(iTJuanPaperQuestionService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除试卷题目
     *
     * @param paperIds 主键串
     */
    @SaCheckPermission("juan:juanPaperQuestion:remove")
    @Log(title = "试卷题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{paperIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] paperIds) {
        return toAjax(iTJuanPaperQuestionService.deleteWithValidByIds(Arrays.asList(paperIds), true) ? 1 : 0);
    }
}
